<?php

namespace app\admin\logic\zhxy\Business;

use app\admin\logic\Admin;
use app\admin\logic\WorkFlow;
use app\admin\logic\workflow\Helper;
use app\admin\logic\zhxy\Business;
use app\admin\logic\zhxy\InterfaceBusiness;
use app\admin\logic\zhxy\task\Inst;
use app\admin\logic\zhxy\task\Task;
use app\admin\model\WfRunProcess;
use think\Env;

/**
 * 业务处理器 驱动基类
 *
 */
abstract class AbstractBusiness implements InterfaceBusiness
{

    /**
     * 业务处理器
     *
     * @var Business
     */
    protected $business;

    static protected $domain = 'https://cgpt.qau.edu.cn/cas/index.php?url=';

    protected $model;

    public function __construct($model, $business)
    {
        $this->model    = $model;
        $this->business = $business;
    }

    /**
     * 获取业务 数据模型
     *
     * @return \app\admin\model\Intention|object
     */
    public function getModel()
    {
        return $this->model;
    }


    abstract public function getAppName(): String;

    abstract public function getUrl(): string;

    abstract public function getInstName(): String;


    /**
     * 创建一个inst
     *
     * @param WfRunProcess $current_pro
     * @return Inst
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function createInst($current_pro, $status)
    {
        $node_ids   = [];
        $node_names = [];

        $masterPor = \app\admin\logic\workflow\WfRunProcess::getMasterProsess($current_pro);

        $allPro    = $this->business->getAllProcess();

        $percent = Helper::calculation_percent($current_pro->process_id, $current_pro->run_flow_process, $allPro);

        foreach ($allPro as $item) {
            if ($item->run_flow_process == $current_pro->run_flow_process) {
                $node_ids[]   = $item->run_flow_process;
                $node_names[] = $item->sponsor_text;
            }
        }
        $inst = new Inst();
        $inst->status  = $status;
        $inst->app_id  = $masterPor->application_id;
        $inst->inst_id = $masterPor->application_id ; //
        $inst->task_id =$current_pro->task_id;

        $debugMenber   = Env::get('zhxy.debug_member', '');
        // 创建人 = 负责人Id
        if ($debugMenber) {
            $inst->creator = $debugMenber;
        } else {
            $inst->creator = Admin::id2username($masterPor->create_admin_id);
        }
        if ($current_pro->bl_time) {
            $inst->finished = date(Task::DATE_F, $current_pro->bl_time);
        }
        if ($current_pro->js_time) {
            $inst->created = date(Inst::DATE_F, $current_pro->js_time);
        } else {
            $inst->created = date(Inst::DATE_F);
        }
        $inst->percent              = $percent;
        $inst->node_name            = $node_names;
        $inst->node_id              = $node_ids;
        $inst->form_url_view        = $this->getUrl();
        $inst->form_mobile_url_view = $this->getUrl();

        $name = $this->getInstName();
        $name = '[采购管理系统]' . $name;

        $inst->name              = $name;
        $inst->conversion_column=[
            'app_id',
            'inst_id',
            'node_id',
            'task_id'
        ];

        return $inst;
    }

    /**
     * @param WfRunProcess $current_pro
     * @return Task
     */
    public function createTask($current_pro, $status)
    {

        $tag = Helper::getTag($this->business->flow_row);

        $task         = new Task();
        $task->status = $status;
        $masterPor = \app\admin\logic\workflow\WfRunProcess::getMasterProsess($current_pro);

        $task->app_id = $masterPor->application_id;
        $task->inst_id = $masterPor->application_id; // depart_id
        // 创建人 = 负责人Id
        $debugMenber = Env::get('zhxy.debug_member', '');
        if ($debugMenber) {
            $task->creator = $debugMenber;
        } else {
            $task->creator = Admin::id2username($this->model->create_admin_id);
        }
        $percent = Helper::calculation_percent($current_pro->process_id, $current_pro->run_flow_process, $this->business->getAllProcess());

        $task->created = date(Task::DATE_F, $current_pro->js_time);
        $task->percent              = $percent;
        $task->node_name            = $current_pro->sponsor_text;
        $task->node_id              = $current_pro->run_flow_process;
        $task->task_id              = $current_pro->task_id;
        $form_url_view              = $this->getUrl();
        $task->form_url_view        = $form_url_view;
        $task->form_mobile_url_view = $form_url_view;
        $task->form_url             = $form_url_view;
        $task->form_mobile_url      = $form_url_view;
        if ($debugMenber) {
            $task->number = [ $debugMenber ];
        } else {
            $adminids = Helper::get_common_uids($this->business->flow_row,$this->model->id,$current_pro);
            $memberids= Admin::ids2memberids($adminids);
            $task->number = $memberids;
        }
        if ($current_pro->bl_time) {
            $task->finished = date(Task::DATE_F, $current_pro->bl_time);
        }
        $task->conversion_column=[
            'app_id',
            'inst_id',
            'task_id',
            'node_id'
        ];

        return $task;

    }

}